package RPPM.dao.impl;

import RPPM.dao.ManagerDao;
import RPPM.domain.*;
import RPPM.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class ManagerDaoImpl implements ManagerDao {
    private JdbcTemplate jdbcTemplate =new JdbcTemplate(JDBCUtils.getDataSource());


    @Override
    public boolean FindExecutor(Executor executor){
        String sql="insert into executor(Exe_id,Exe_name,Exe_password) values (?,?,?)";
        int count=jdbcTemplate.update(sql,
                executor.getExe_id(),
                executor.getExe_name(),
                executor.getExe_password());
        return (count==1)?true:false;
    }

    @Override
    public boolean ProvideTeacherinformation(Teacher teacher) {
        return false;
    }

    @Override
    public List<Map<String, Object>> getStudents() {
        String sql = "SELECT DISTINCT student.Stu_id, student.Stu_name, student.Stu_sex, student.Stu_level, student.Stu_company, student.Stu_post, student.Stu_phone FROM student";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql);
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> getTeachers() {
        String sql = "SELECT Tea_id, Tea_name, Tea_sex, Tea_major, Tea_posts, Tea_phone FROM teacher";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql);
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> getPlans() {
        String sql = "SELECT Cou_id, Cou_name, Cou_content, Cou_location, Cou_teacher, Cou_payment, Com_name, Com_phone, Cou_pass FROM course";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql);
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Manager findManagerById(Manager toBechecked) {
        String sql = "select * from manager where Manager_id = ?";
        Manager mng;
        try {

            mng = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Manager>(Manager.class), toBechecked.getManager_id());
            return mng;

        } catch (Exception e){
            mng = null;
            return mng;
        }
    }

    @Override
    public boolean addPlan(List<Map<String, Object>> dataList) {
        // 假设你的数据库连接已经建立，这里省略连接数据库的代码

        // 遍历datalist中的每个Map对象
        for (Map<String, Object> dataMap : dataList) {
            // 从Map中获取对应的信息
            String couName = (String) dataMap.get("Cou_name");
            String couContent = (String) dataMap.get("Cou_content");
            String couLocation = (String) dataMap.get("Cou_location");
            String couTeacher = (String) dataMap.get("Cou_teacher");
            String couPayment = (String) dataMap.get("Cou_payment");
            String comName = (String) dataMap.get("Com_name");
            String comPhone = (String) dataMap.get("Com_phone");

            boolean res;
            try {
                String sql = "INSERT INTO course (Cou_name, Cou_content, Cou_location, Cou_teacher, Cou_payment, Com_name, Com_phone, Cou_pass) VALUES (?, ?, ?, ?, ?, ?, ?, 0)";
                int rowsAffected = jdbcTemplate.update(sql, couName, couContent, couLocation, couTeacher, couPayment, comName, comPhone);
                res = (rowsAffected > 0);
                return res;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean addTeacher(List<Map<String, Object>> datalist) {
        // 遍历datalist中的每个Map对象
        for (Map<String, Object> dataMap : datalist) {
            // 从Map中获取对应的信息
            String Tea_name = (String) dataMap.get("Tea_name");
            String Tea_sex = (String) dataMap.get("Tea_sex");
            String Tea_major = (String) dataMap.get("Tea_major");
            String Tea_posts = (String) dataMap.get("Tea_posts");
            String Tea_phone = (String) dataMap.get("Tea_phone");

            boolean res;
            try {
                String sql = "INSERT INTO teacher (Tea_name, Tea_sex, Tea_major, Tea_posts, Tea_phone) VALUES (?, ?, ?, ?, ?)";
                int rowsAffected = jdbcTemplate.update(sql, Tea_name, Tea_sex, Tea_major, Tea_posts, Tea_phone);
                res = (rowsAffected > 0);
                return res;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean deleteStudent(int stuid) {
        boolean res;
        try {
            String sql = "DELETE FROM student WHERE Stu_id = ?";
            int rowsAffected = jdbcTemplate.update(sql, stuid);
            res = (rowsAffected > 0);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteTeacher(int teaid) {
        boolean res;
        try {
            String sql = "DELETE FROM teacher WHERE Tea_id = ?";
            int rowsAffected = jdbcTemplate.update(sql, teaid);
            res = (rowsAffected > 0);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deletePlan(int couid) {
        boolean res;
        try {
            String sql = "DELETE FROM course WHERE Cou_id = ?";
            int rowsAffected = jdbcTemplate.update(sql, couid);
            res = (rowsAffected > 0);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Map<String, Object>> findTeacher(String searchText) {
        String sql = "SELECT Tea_id, Tea_name, Tea_sex, Tea_major, Tea_posts, Tea_phone FROM teacher WHERE Tea_name like ?";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, "%"+searchText+"%");
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> findStudent(String searchText) {
        String sql = "SELECT DISTINCT student.Stu_id, student.Stu_name, student.Stu_sex, student.Stu_level, student.Stu_company, student.Stu_post, student.Stu_phone FROM student WHERE Stu_name like ?";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, "%"+searchText+"%");
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> findPlan(String searchText) {
        String sql = "SELECT Cou_id, Cou_name, Cou_content, Cou_location, Cou_teacher, Cou_payment, Com_name, Com_phone, Cou_pass FROM course WHERE Cou_name like ?";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, "%"+searchText+"%");
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean changeStudent(int stuid, String column, String change) {
        boolean res;
        try {
            String sql = "UPDATE student SET " + column + " = ? WHERE Stu_id = ?";
            int rowsAffected = jdbcTemplate.update(sql, change, stuid);
            res = (rowsAffected > 0);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean changeTeacher(int teaid, String column, String change) {
        boolean res;
        try {
            String sql = "UPDATE teacher SET " + column + " = ? WHERE Tea_id = ?";
            int rowsAffected = jdbcTemplate.update(sql, change, teaid);
            res = (rowsAffected > 0);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean changePlan(int couid, String column, String change) {
        boolean res;
        try {
            String sql = "UPDATE course SET " + column + " = ? WHERE Cou_id = ?";
            int rowsAffected = jdbcTemplate.update(sql, change, couid);
            res = (rowsAffected > 0);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean addStudent(List<Map<String, Object>> datalist) {
        // 遍历datalist中的每个Map对象
        for (Map<String, Object> dataMap : datalist) {
            // 从Map中获取对应的信息
            String Stu_name = (String) dataMap.get("Stu_name");
            String Stu_sex = (String) dataMap.get("Stu_sex");
            String Stu_level = (String) dataMap.get("Stu_level");
            String Stu_company = (String) dataMap.get("Stu_company");
            String Stu_post = (String) dataMap.get("Stu_post");
            String Stu_phone = (String) dataMap.get("Stu_phone");

            boolean res;
            try {
                String sql = "INSERT INTO student (Stu_name, Stu_sex, Stu_level, Stu_company, Stu_post, Stu_phone) VALUES (?, ?, ?, ?, ?, ?)";
                int rowsAffected = jdbcTemplate.update(sql, Stu_name, Stu_sex, Stu_level, Stu_company, Stu_post, Stu_phone);
                res = (rowsAffected > 0);
                return res;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
